/**
 * @param {number[]} candidates
 * @param {number} target
 * @return {number[][]}
 */

var combinationSum = function (candidates, target) {
    // 原数组长度
    let len = candidates.length
    // 结果数组
    let res = []
    // 运营数组
    let arr = []
    backtrace(0)
    return res
    function backtrace(index) {
        let sum = 0
        for (let i = 0; i < arr.length; i++) {
            sum += arr[i]
        }
        // 如果超出了直接返回
        if (sum > target) {
            return
        }
        // 如果arr中值相加等于target 则推入res
        if (sum === target) {
            res.push(arr.slice())
            return
        }
        for (let i = index; i < len; i++) {
            arr.push(candidates[i])
            backtrace(i)
            arr.pop()
        }
    }
};